function initCommonFunction(){
    preventEnterSubmit();
}

function setDatePicker(){
    if($('.input-date').length > 0){
        $(document.body).on('focus', '.input-date', function(){
            $(this).datepicker({dateFormat: 'yy-mm-dd',changeMonth: true,changeYear: true});
        });
    }
}

function imgPreview(){
    if($.fancybox){
        $('.fancybox').fancybox({padding:0,openEffect:'elastic',closeEffect:'elastic'});
    }
}

function resetForm(form){
    $(form + ' input, ' + form + ' textarea').val('');
    $(form + ' select').prop('selectedIndex', 0);
}

function setMessage(data){
	var alert_class = 'alert-success';
	var alert_title='';
	var alert_text=data.text;
	if(data.code!=1){
		alert_class = 'alert-danger';
		var alert_title=data.title;
	}
	$('div#flg_message').removeClass('alert-danger').removeClass('alert-success').addClass(alert_class).removeClass('hidden');
	$('div#flg_message .message-title').html(alert_title);
	$('div#flg_message .message-text').html(alert_text);
}

function flashMessage(msgDiv, text){
    msgDiv.html(text)
    .removeClass('hidden')
    .hide()
    .slideDown();
    setTimeout(function(){ msgDiv.slideUp().addClass('hidden'); }, 1000);
}

function htmlScrollTo(element){
    var position = $(element).offset().top;
    $("html, body, #content").animate({ scrollTop: position }, 600);
}

function htmlScrollToMessage(){
    htmlScrollTo('#flg_message');
}

function preventEnterSubmit(){
    $(document.body).on('keydown', 'input[type=text], input[type=password]', function(event){
        if($(this).hasClass('can-submit')){
            if(event.keyCode == 13) {
                var btn = $(this).attr('data-follow-submit');
                $(btn).trigger('click');
            }
        } else {
            var closestForm = $(this).closest('form');
            if(closestForm.length!=0 && closestForm.attr('has-validate')!=undefined){
                if(event.keyCode == 13) {
                    event.preventDefault();
                    return false;
                }
            }
        }
    });
}

function formatCurrency(val){
    var result = "";
    var a = $.trim(val.toString()).split('').reverse();
    for(var i=0;i<a.length;i++){
        if(i%3==0){
            result += '.';
        }
        result += a[i].toString();
    }
    var a1 = result.split('.').reverse();
    result="";
    for(var i=0;i<a1.length;i++){
        var a2 = a1[i].split('').reverse().join('');
        result += a2.toString() + '.';
    }
    result = result.replace('..','');
    result += "<sup>đ</sup>"
    return result;
}

function changeTabPane(){
    $('#tabs a').click(function (e) {
        e.preventDefault()
        $(this).tab('show');
        return false;
    });
}

function wireOfAction(element, action, callback){
    if(callback!==undefined){
        $(document.body).on(action, element, function(){ callback(); });
    } else {
        $(element).trigger(action);
    }
}